﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

namespace Hangjing.WebCommon
{
    /// <summary>
    /// 商家处理订单类
    /// </summary>
    public class shopSetOrder
    {
        HttpContext context;
        int shopid = 0;
        int ishopset = 0;
        string orderid = "";
        string resign = "";//拒绝，取消理由


        /// <summary>
        /// 商家处理订单
        /// </summary>
        /// <param name="_context"></param>
        /// <param name="shopid">可以表示商家编号、骑士编号、群组编号</param>
        public shopSetOrder(HttpContext _context, int _shopid, string _orderid, int _ishopset, string _resign)
        {
            context = _context;
            shopid = _shopid;
            orderid = _orderid;
            ishopset = _ishopset;
            resign = _resign;
        }

        /// <summary>
        /// 处理订单,返回0表示这个已经处理了过了，1表示处理成功
        /// </summary>
        public int HandleOrder()
        {
            int rs = 1;
            Custorder dal = new Custorder();
            CustorderInfo model = dal.GetModel(orderid);


            string sql = "";
            switch (ishopset)
            {
                case 1:

                    dal.AddOrderRecord(model.orderid, model.OrderStatus, "商家", "商家接收订单:订单详情");
                    sql = " UPDATE dbo.Custorder SET  ReveDate1 = getdate(),IsShopSet=1  where unid = " + model.Unid;
                    Hangjing.DBUtility.SQLHelper.excutesql(sql);

                    new OrderStep().Add(new OrderStepInfo()
                    {
                        stepcode = 20,
                        title = "商家已经接单",
                        subtitle = "",
                        addtime = DateTime.Now,
                        orderid = orderid,
                        revevar1 = "",
                        revevar2 = ""

                    });

                    //自动调度订单 通知骑士
                    if (model.ReveVar1 == "0")
                    {
                        IList<ROrderinfo> my_list = new List<ROrderinfo>();
                        ROrderinfo mm = new ROrderinfo();
                        mm.latlng = model.ReveVar2;
                        mm.Orderid = model.orderid;
                        mm.cityid = model.cityid;
                        mm.sentorg = model.ReveVar1;


                        mm.cardpay = 0;
                        mm.latlng = model.ReveVar2;
                        mm.sentorg = "0";
                        mm.cityid = model.cityid;
                        mm.groupid = model.deliverheaderid;
                        mm.Orderid = model.orderid;




                        my_list.Add(mm);
                        dal.AutoDispatch(my_list);
                    }


                    break;
                case 2:

                    dal.AddOrderRecord(model.orderid, 4, "商家", "商家拒绝订单:订单详情");
                    sql = " UPDATE dbo.Custorder SET  ReveDate1 = getdate(),IsShopSet=2,OrderStatus=4,OrderAddrEx ='" + resign + "' where unid = " + model.Unid;
                    Hangjing.DBUtility.SQLHelper.excutesql(sql);

                    dal.OrderCancelRefundForShop(model.orderid);

                    new OrderStep().Add(new OrderStepInfo()
                    {
                        stepcode = 80,
                        title = "商家拒绝订单",
                        subtitle = "",
                        addtime = DateTime.Now,
                        orderid = orderid,
                        revevar1 = "",
                        revevar2 = ""

                    });

                    break;
            }

            NoticeHelper notice = new NoticeHelper(context, shopid.ToString());
            notice.send2ShopByShopid();

            return rs;

        }

        /// <summary>
        /// 完成订单
        /// </summary>
        /// <returns></returns>
        public int CompleteOrder()
        {
            int rs = 0;
            Custorder dal = new Custorder();
            CustorderInfo model = dal.GetModel(orderid);

            dal.AddOrderRecord(model.orderid, 3, "商家", "订单详细界面");
            if (dal.UpdataState(model.Unid, 3) > 0)
            {
                dal.AddPoint(model.orderid.ToString());
                rs = 1;

                new OrderStep().Add(new OrderStepInfo()
                {
                    stepcode = 70,
                    title = "订单完成",
                    subtitle = "",
                    addtime = DateTime.Now,
                    orderid = orderid,
                    revevar1 = "",
                    revevar2 = ""

                });

            }
            else
            {
                rs = 0;
            }

            return rs;
        }


    }
}